﻿using FrameworkDesign;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace CounterApp
{
    public class CounterViewController : MonoBehaviour
    {

     

        // Start is called before the first frame update
        void Start()
        {
            CounterModel.Count.onValueChanged += onCountChanged;
            onCountChanged(CounterModel.Count.Value);

            transform.Find("BtnP").GetComponent<Button>().onClick.AddListener(() =>
            {
                new AddCountCommand().Execute();
           
            });

            transform.Find("BtnM").GetComponent<Button>().onClick.AddListener(() =>
            {
                new SubCountCommand().Execute();
            
            });
        }

        void updateView()
        {
            transform.Find("CountNum").GetComponent<Text>().text = CounterModel.Count.Value.ToString();
        }

        void onCountChanged(int newCount)
        {
            transform.Find("CountNum").GetComponent<Text>().text = newCount.ToString();
        }

        private void OnDestroy()
        {
            CounterModel.Count.onValueChanged -= onCountChanged;
        }
    }

    public static class CounterModel
    {
        public static BindableProperty<int> Count = new BindableProperty<int>() {
            Value = 0
        };
    }
}

